package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavourMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static org.omg.IOP.TaggedProfileHelper.insert;

@Service
@Slf4j
public class DishServiceimpl implements DishService {
@Autowired
   private DishMapper dishMapper;
@Autowired
private DishFlavourMapper dishFlavourMapper;
@Autowired
private SetmealDishMapper setmealDishMapper;
@Transactional
    @Override
    public void saveWithFlavor(DishDTO dto) {
//菜品
    Dish dish =new Dish();

    BeanUtils.copyProperties(dto,dish);


      dishMapper.insert(dish);

//菜品口味
    Long id = dish.getId();
    List<DishFlavor> flavors = dto.getFlavors();
    if (flavors!=null&&flavors.size()>0) {
        flavors.forEach(dishFlavor -> {dishFlavor.setDishId(id);});
            dishFlavourMapper.insertBench(flavors);



    }


}

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());


        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);


        return new PageResult(page.getTotal(), page.getResult());
    }


    @Transactional
    @Override
    public void deleteBench(List<Long> ids) {


//判断当前菜品是否起售


        for (Long id : ids) {
          Dish dish =   dishMapper.getById(id);
if(dish.getStatus()== StatusConstant.ENABLE){

    //当前菜品起售不能删除
    throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);

}

        }

        //判断当前菜品是否关联
        List<Long> setmealDishIds = setmealDishMapper.getSetmealDishIds(ids);

if(setmealDishIds !=null&&setmealDishIds.size()>0){

    //当前菜品被关联，不能删除
    throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);



}
        //删除菜品表中数据
        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除菜品关联表的口味数据
            dishFlavourMapper.deleteByDishId(id);
        }









    }

    @Override
    public DishVO getByid(Long id) {
        Dish byId = dishMapper.getById(id);
      List<DishFlavor> dishFlavors=  dishFlavourMapper.getByid(id);
      DishVO dishVO =new DishVO();
BeanUtils.copyProperties(byId,dishVO);
dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updateDishwithFlavor(DishDTO dto) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dto,dish);
    //修改基本信息
    dishMapper.updatebasemessage(dish);

//修改口味
        dishFlavourMapper.deleteByDishId(dto.getId());
        //先删除原有的口味，再添加新的口味
        List<DishFlavor> flavors = dishFlavourMapper.getByid(dto.getId());
        if (flavors!=null&&flavors.size()>0) {
            flavors.forEach(dishFlavor -> {dishFlavor.setDishId(dto.getId());});
            dishFlavourMapper.insertBench(flavors);



        }


    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavourMapper.getByid(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
